/*
 * 新增轮播图
 * @Author: LuoQianJun
 * @Date: 2018-01-26 11:31:53 
 * @Last Modified by: LuoQianJun
 * @Last Modified time: 2018-01-30 10:06:15
 */
<style lang="less" rel="stylesheet/less" type="text/less" scoped>

    .v-admin-carousel-create {
        min-height: 600px;
    }
    
</style>
<template>
<div class="v-admin-carousel-create">
    <Row type="flex" justify="start">
        <Col push="1">
            <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="80">

                <Form-item label="图片" prop="image">
                    <Upload :before-upload="file=>fileChange(file,'image','image_info')"
                            :on-remove="file=>fileChange(file,'image','image_info')"
                            :multiple="false"
                            accept="image/jpg, image/jpeg, image/png"
                            action="//fake_upload_url/">
                        <Button type="ghost" icon="ios-cloud-upload-outline">上传图片</Button>
                        <div class="license-tip" slot="tip" v-if="image_info.name">{{ image_info.name }}</div>
                        <!-- <div class="license-tip" slot="tip" v-else>图片格式为jpg、jpeg或png，大小不超1.5M</div> -->
                    </Upload>
                </Form-item>

                <Form-item label="优先级" prop="priority">
                    <InputNumber :max="9999" :min="0" v-model="formValidate.priority" placeholder="请输入优先级"></InputNumber>
                </Form-item>
              
                <Form-item>
                    <Button type="primary" @click="handleSubmit('formValidate')">提交</Button>
                    <Button type="ghost" @click="handleReset('formValidate')" style="margin-left: 8px">重置</Button>
                </Form-item>

            </Form>
        </Col>
    </Row>
</div>
</template>

<script>

import { mapGetters } from 'vuex';

import {validateImage} from 'src/validate'; 
    
export default {
    name: 'carousel-create',
    data() {	
        return {

            formValidate : {

                priority : 0,

                image : '',

            },                  //新增轮播图表单信息

            ruleValidate: {

                image: [

                    { required: true, validator: validateImage, trigger: 'blur,change' }

                ],

                priority: [

                    { required: true, type: 'number', message: '优先级不能为空', trigger: 'change' },
                    
                ],
               
            },

             // 上传的图片信息
            image_info : {
                
                name: '',
               
            }

        }
    },
    methods: {
        
        /**
         * file类型input改变
         * @param  {Object} file {选择的文件对象}
         * @return {Boolean} {false则会停止上传}
         */
        
        fileChange(file,param,info){

            this[info].name = file.name;

            this.formValidate[param] = file;

            return false;

        },


         /**
         * 提交表单
         * @param  {string} name {表单的ref}
         * @return {undefined}
         */
        handleSubmit (name) {
            this.$refs[name].validate((valid) => {
                if (valid) {

                    this.$emit('post',this.formDataCreate());

                } else {

                    this.$Message.error('存在未填写的条目!');

                }
            })
        },

        /**
         * 重置表单
         * @param  {string} name {表单的ref}
         * @return {undefined} 
         */
        handleReset (name) {

            this.$refs[name].resetFields();

            this.formValidate.image = '';

            this.formValidate.priority = 0;

            this.image_info.name = '';

        },

        /**
         * 手动拼接formdata数据
         * @return {Object} {拼接好的formdata数据}
         */
        formDataCreate(){

            const new_object = {
           
                image : this.formValidate.image,

                priority : this.formValidate.priority,


            };

            let form_data = new FormData();

            Object.keys(new_object).forEach((key)=>{

                form_data.append( key , new_object[key] );
        
            });

            return form_data;

        }

        
    }
}
</script>